'use strict'

const { Controller } = require('egg')

class AdminController extends Controller {
  /**
   * 系统首页
   */
  async home() {
    const { ctx } = this
    const { admin } = ctx.state

    const adminRole = await ctx.model.AdminRole.findOne({
      _id: admin.roleId,
    })

    const menuFilter = {
      // systemType: admin.systemType,
      isOpen: true,
      isHide: false,
      type: 0,
    }

    // 指定菜单
    if (!adminRole.isAdmin) menuFilter._id = adminRole.menuIds

    let [menuList, defaultMenu, systemConfig] = await Promise.all([
      ctx.model.AdminMenu.tree({
        filter: menuFilter,
        sort: {
          isTop: 'Desc',
          rank: 'ASC',
        },
      }),
      ctx.model.AdminMenu.findOne({
        ...menuFilter,
        $and: [{ path: { $ne: null } }, { path: { $ne: '' } }],
      }).sort({
        isTop: 'Desc',
        rank: 'ASC',
      }),
      ctx.service.config.getConfigByType('system'),
    ])

    if (!defaultMenu) defaultMenu = menuList[0]

    ctx.body = {
      code: 0,
      msg: 'success',
      data: {
        menuList,
        defaultMenu,
        systemConfig,
      },
    }
  }

  // 获取系统配置
  async getSystemSetting() {
    const { ctx } = this

    const returnData = await ctx.service.config.getConfigByType('system')

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  // 设置系统配置
  async setSystemSetting() {
    const { ctx } = this
    const configType = 'system'

    await ctx.model.Config.updateOne(
      { configType },
      { configType, config: ctx.request.body },
      {
        upsert: true, // 不存在插入
        setDefaultsOnInsert: true, // 允许插入默认值
      }
    )

    // await this.service.config.deleteConfigCache(configType)

    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }

  constant() {
    const { ctx } = this
    const CONSTANT = this.config.constant
    const returnData = {
      ...CONSTANT,
    }

    delete returnData.SYSTEM_TYPE_ADMIN
    delete returnData.SYSTEM_TYPE_STORE
    delete returnData.ADMIN_PASSWORD

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  async dictList() {
    const { ctx } = this
    const { query } = ctx

    const returnData = await this.service.dict.getDict()
    // const returnData = await this.config.dict

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  async constantList() {
    const { ctx } = this
    const { query } = ctx

    const returnData = await this.config.constant

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  async dictDetail() {
    const { ctx } = this
    const { query } = ctx
    const isArray = query.query != false ? true : false
    const key = this.ctx.params.id

    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: this.service.dict[isArray ? 'getDictArr' : 'getDict'](key),
    }
  }

  /**
   * @apiDefine AdminCommon
   * @apiVersion 0.0.0
   *
   * @apiHeader {String} Authorization 用户token
   *
   * @apiSuccess {Number} code 状态码 <br /> 0、成功 <br /> 1、失败
   * @apiSuccess {String} msg  消息
   * @apiSuccess {Object} data 返回数据
   *
   */
}

module.exports = AdminController
